package Classes
{
	public class Util
	{
		public function Util()
		{
		}
		
		public static function secureRand(min:int,max:int):int{
			var rand:Number = Math.random();
			return Math.round(min + (max-min)*rand);
		}
		
		public static function xml2obj(xmlNode:XML):Object{
			var attributes:Object = getAttributes(xmlNode);
			var obj:Object = new Object;
			var children:Object = new Object;
			var childrenByName:Object = new Object;
			
			for each(var child:XML in xmlNode.children()){
				var childName:String = child.name();
				if(!children[childName]){
					children[childName] = []
				}
				children[childName].push(xml2obj(child));
				
				if(child.@name){
					childrenByName[child.@name] = xml2obj(child);
				}
			}

			obj['attributes'] = attributes;
			obj['children'] = children;
			obj['childrenByName'] = childrenByName;
			
			return obj;
		}
		
		private static function getAttributes(xmlNode:XML):Object{
			var attributes:Object = new Object();
			for each(var attr:XML in xmlNode.attributes()){
				var attrName:String = attr.name();
				attributes[attrName] = isNaN(attr.valueOf()) ? String(attr.valueOf()) : Number(attr.valueOf());
			}
			
			return attributes;
		}
		
	}
}